package com.ztgf.pay.service;

import com.alibaba.fastjson.JSON;
import com.ztgf.dubbo.exception.ServiceException;
import com.ztgf.order.models.bo.OrderPayBO;
import com.ztgf.order.models.service.IOrderPayRelationService;
import com.ztgf.order.models.service.pay.IPayOrderLogService;
import com.ztgf.pay.async.IAsyncService;
import com.ztgf.pay.models.dto.PayOrderDTO;
import com.ztgf.pay.models.dto.PayUserRechargeDTO;
import com.ztgf.shopping.models.bo.shop.ShopAnnualFeeReturnBO;
import com.ztgf.shopping.models.bo.shop.ShopEarnestMoneyReturnBO;
import com.ztgf.shopping.models.service.shop.IShopAnnualFeeService;
import com.ztgf.shopping.models.service.shop.IShopEarnestMoneyService;
import com.ztgf.shopping.models.service.shop.IShopPayRefundLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.scheduling.annotation.Async;

@DubboService
@Slf4j
public class AsyncServiceImpl implements IAsyncService {
    @DubboReference
    private IShopAnnualFeeService shopAnnualFeeService;

    @DubboReference
    private IOrderPayRelationService orderPayRelationService;

    @DubboReference
    private IShopPayRefundLogService shopPayRefundLogService;

    @DubboReference
    private IShopEarnestMoneyService shopEarnestMoneyService;

    @DubboReference
    private IPayOrderLogService payOrderLogService;

    @Async
    @Override
    public void notifyOrder(OrderPayBO orderPayBO) {
        orderPayRelationService.addOrderPayRelationInfo(orderPayBO);
    }

    @Async
    @Override
    public void notifyAnnualFee(Long orderId, Long payOrderId, int status) {
        shopPayRefundLogService.updateRefund(orderId + "", payOrderId + "", 1);
    }

    @Async
    @Override
    public void notifyAnnualFee(ShopAnnualFeeReturnBO bo) {
        shopAnnualFeeService.updatePaidReturn(bo);
    }

    @Async
    @Override
    public void notifyMargin(ShopEarnestMoneyReturnBO bo) {
        shopEarnestMoneyService.updatePaidReturn(bo);
    }

    @Override
    public void notifyPayOrder(PayOrderDTO orderDTO) {
        try{
            log.error("-------------网银支付回调-------------------");
            payOrderLogService.savePaySuccessNotify(orderDTO);
        }catch (ServiceException e){
            log.error("支付回调错误 is error " + JSON.toJSONString(orderDTO), e);
        } catch (RuntimeException e){
            log.error("支付回调错误RuntimeException is error " + JSON.toJSONString(orderDTO), e);
        } catch (Exception e){
            log.error("支付回调错误Exception is error " + JSON.toJSONString(orderDTO), e);
        }

    }

    @Override
    public void notifyMargin(PayUserRechargeDTO rechargeDTO) {
        try{
            log.error("-------------充值回调-------------------");
            payOrderLogService.saveRechargeSuccessNotify(rechargeDTO);
        }catch (ServiceException e){
            log.error("充值回调错误 is error " + JSON.toJSONString(rechargeDTO), e);
        } catch (RuntimeException e){
            log.error("充值回调错误RuntimeException is error " + JSON.toJSONString(rechargeDTO), e);
        } catch (Exception e){
            log.error("充值回调错误Exception is error " + JSON.toJSONString(rechargeDTO), e);
        }
    }
}
